package com.aaa.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.entity.Result;
import com.aaa.utils.MyConstants;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：Teacher陈（86521760@qq.com）
 * @date ：Created in 2022/8/9 9:22
 * @description：所有的controller的基础类
 * @modified By：
 * @version: 1.0
 */
public class BaseController extends HttpServlet {
    /**
     * @param req
     * @return java.util.Map
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 将所有的请求参数封装放到一个map中
     * @create time: 2022/8/9 9:26
     */
    public Map getParams(HttpServletRequest req) {

        Map map = new HashMap<>();
        Enumeration<String> parameterNames = req.getParameterNames();
        while (parameterNames.hasMoreElements()) {
            String key = parameterNames.nextElement();
            String value = req.getParameter(key);
            if (ObjectUtil.isEmpty(value)) {
                continue;
            }
            map.put(key, value);
        }
        return map;
    }


    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 响应json数据到页面
     * @create time: 2022/8/11 9:13
     * @param result
     * @param resp
     * @return void
     */
    public  void responseJson(Object result,HttpServletResponse resp) throws IOException {

        //构建json配置对象
        JSONConfig jsonConfig = JSONConfig.create();
        //设置时间格式
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        //java对象转json字符串
        String jsonStr = JSONUtil.toJsonStr(result,jsonConfig );
        resp.getWriter().write(jsonStr);
    }

    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 返回成功，不携带数据，适用于增删改
     * @create time: 2022/8/11 9:20
     * @param resp
     * @return void
     */
    public  void  success(HttpServletResponse resp) throws IOException {
        Result result = new Result(MyConstants.SUCCESS_CODE,MyConstants.SUCCESS_MSG,null,null);
        this.responseJson(result,resp);
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 返回成功，不携带数据，适用于增删改
     * @create time: 2022/8/11 9:20
     * @param resp
     * @return void
     */
    public  void  fail(HttpServletResponse resp) throws IOException {
        Result result = new Result(MyConstants.FAIL_CODE,MyConstants.FAIL_MSG,null,null);
        this.responseJson(result,resp);
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 携带特殊错误信息
     * @create time: 2022/8/11 9:30
     * @param resp
     * @param msg
     * @return void
     */
    public  void  fail(HttpServletResponse resp,String msg) throws IOException {
        Result result = new Result(MyConstants.FAIL_CODE,msg,null,null);
        this.responseJson(result,resp);
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 操作成功，需要返回数据，例如  添加的时候返回主键
     * @create time: 2022/8/11 9:28
     * @param resp
     * @param data
     * @return void
     */
    public  void  success(HttpServletResponse resp,Object data) throws IOException {
        Result result = new Result(MyConstants.SUCCESS_CODE,MyConstants.SUCCESS_MSG,null,data);
        this.responseJson(result,resp);
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 分页查询
     * @create time: 2022/8/11 9:27
     * @param resp
     * @param data
     * @param count
     * @return void
     */
    public  void  success(HttpServletResponse resp,Object data,Long count) throws IOException {

        Result result = new Result(MyConstants.SUCCESS_CODE,MyConstants.SUCCESS_MSG,count,data);
        this.responseJson(result,resp);
    }
    /**
     * @create by: Teacher陈（86521760@qq.com）
     * @description: 需要特殊提示信息的
     * @create time: 2022/8/11 9:27
     * @param resp
     * @param msg
     * @return void
     */
    public  void  success(HttpServletResponse resp,String msg) throws IOException {
        Result result = new Result(MyConstants.SUCCESS_CODE,msg,null,null);
        this.responseJson(result,resp);
    }
}
